<template>
  <div class="employees">
    <!-- 使用工具栏组件 -->
    <pageTools :showleft="true">
      <template #left> 员工管理 </template>
      <template #right>
        <!-- <el-button type="primary" size="mini">导入</el-button> -->
        <el-button @click="toImport" type="primary" size="mini"
         :disabled="'employees_import' | hasBtnRight"
          >批量导入</el-button
        >
        <el-button type="primary" size="mini" @click="openadd"
           :disabled="'employees_add' | hasBtnRight"
          >+新增员工</el-button
        >
      </template>
    </pageTools>

    <!-- 新增面板 -->
    <mypanel
      :dialogVisible.sync="dialogVisible"
      @updateData="getEmployeesList"
    ></mypanel>
    <!-- 预览图片 -->
    <el-dialog title="预览图片" :visible.sync="imgshow" class="mydialog">
      <!-- <img :src="mysrc" alt=""> -->
      <!-- 添加一个转换后显示的容器 -->
      <canvas ref="mycanvas"></canvas>
    </el-dialog>
    <!-- 分配角色面板 -->
    <el-dialog title="分配角色" :visible.sync="roleShow" @close="cancel">
      <div>{{ roleList }}</div>
      <br />
      <el-checkbox-group v-model="roleList">
        <el-checkbox
          v-for="(item, index) in allRole"
          :key="index"
          :label="item.id"
          >{{ item.name }}</el-checkbox
        >
      </el-checkbox-group>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 表格区域 -->
    <el-card class="box-card">
      <el-table :data="tableData" border class="mytable">
        <el-table-column
          label="序号"
          type="index"
          :index="indexFn"
          width="60px"
        ></el-table-column>
        <el-table-column
          prop="username"
          width="100px"
          sortable
          label="姓名"
        ></el-table-column>
        <el-table-column sortable label="图片">
          <template slot-scope="{ row }">
            <img
              :src="row.staffPhoto"
              class="myimg"
              v-imgerror="defaultImg"
              @click="previewImg(row.staffPhoto)"
            /> </template
        ></el-table-column>
        <el-table-column
          prop="mobile"
          width="120px"
          sortable
          label="手机号"
        ></el-table-column>
        <el-table-column
          prop="workNumber"
         width="80px"
          sortable
          label="工号"
        ></el-table-column>
        <el-table-column prop="formOfEmployment" width="80px" label="聘用形势">
          <template slot-scope="{ row }">
            {{ row.formOfEmployment | myEmployees }}
          </template>
        </el-table-column>
        <el-table-column
          prop="departmentName"

          sortable
          label="部门"
        ></el-table-column>
        <el-table-column width="120px" sortable label="入职时间">
          <template slot-scope="{ row }">
            {{ row.timeOfEntry | mymoment }}
          </template>
        </el-table-column>
        <el-table-column width="80px" label="是否在职">在职</el-table-column>
        <el-table-column width="80px" label="状态">可用</el-table-column>
        <el-table-column width="300px" label="操作">
          <template slot-scope="{ row }">
             <el-button type="text" @click="toDetail(row)"
           :disabled="'employees_check' | hasBtnRight"
            >查看</el-button>
            <el-button type="text"
            :disabled="'employees_currect' | hasBtnRight"
            >转正</el-button>
            <el-button type="text"
             :disabled="'employees_transfer' | hasBtnRight"
            >调岗</el-button>
            <el-button type="text"
               :disabled="'employees_leave' | hasBtnRight"
            >离职</el-button>
            <el-button type="text" @click="openRole(row.id)">角色</el-button>
            <el-button type="text">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页组件 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="pageSizes"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import {
  getEmployeesListApi,
  getBaseInfoApi,
  setRoleApi,
} from "@/api/employees";
import { getAllRoleApi } from "@/api/roles";
// 导入对象
import QRcode from "qrcode";
export default {
  data() {
    return {
      // 表格的数据源
      tableData: [],
      // 当前页
      currentPage: 1,
      // 页容量的集合
      pageSizes: [5, 10, 15, 20],
      // 页容量
      pageSize: 10,
      // 总条数
      total: 0,
      // 控制面板的显示与隐藏
      dialogVisible: false,
      // 设置一张默认图片
      defaultImg: require("@/assets/common/img.jpeg"),
      // 控制预览面板的显示与隐藏
      imgshow: false,
      // 预览面板中的图片路径
      mysrc: "",
      // 控制角色面板的显示与隐藏
      roleShow: false,
      // 选中的角色的数组
      roleList: [],
      // 所有的角色列表
      allRole: [],
      // 当前操作员工的id
      currentId: "",
    };
  },
  methods: {
    async submit() {
      await setRoleApi({
        id: this.currentId,
        roleIds: this.roleList,
      });
      //  提示修改成功
      this.$message({
        message: "修改成功",
        type: "success",
      });
      // 关闭面板
      this.roleShow = false;
      // 更新数据
      this.getEmployeesList();
    },

    // 获取所有的角色列表
    async getAllRole() {
      const res = await getAllRoleApi();
      // console.log(res);
      // 保存所有的角色列表
      this.allRole = res.data.rows;
    },
    // 关闭角色面板
    cancel() {
      this.roleShow = false;
    },
    // 打开角色面板
    async openRole(id) {
      // console.log(id);
      // id 当前点击数据的id
      // 保存当前员工的id
      this.currentId = id
      this.roleShow = true;
      // 得到当前点击员工的角色信息
      const res = await getBaseInfoApi(id);
      // console.log(res);
      // 保存当前点击员工的角色信息
      this.roleList= res.data.roleIds
    },
    previewImg(src) {
      (this.imgshow = true),
        (this.mysrc = src),
        this.$nextTick(() => {
          // 将mysrc转为二维码
          QRcode.toCanvas(
            this.$refs.mycanvas, //生成二维码后保存的容器
            this.mysrc, //要转换的路径
            {
              // 相关配置信息
              width: 200,
              height: 200,
            }
          );
        });
    },
    // 跳转到详情页面
    toDetail(row) {
      // console.log(row);
      this.$router.push(`/employees/detail?id=${row.id}`);
    },
    // 跳转到导入页面
    toImport() {
      this.$router.push("/employees/import");
    },
    // 第二页的序号接上第一页的序号
    indexFn(index) {
      // 第一页=> 1
      // 第二页=> 2
      return (this.currentPage - 1) * this.pageSize + index + 1;
    },

    // 打开新增面板
    openadd() {
      this.dialogVisible = true;
    },

    // 当页容量改变时会触发
    handleSizeChange(size) {
      // console.log(`每页 ${size} 条`);
      (this.pageSize = size), this.getEmployeesList();
    },
    // 当前页改变时会触发
    handleCurrentChange(page) {
      // console.log(`当前页: ${page}`);
      (this.currentPage = page), this.getEmployeesList();
    },

    // 获取所有的员工信息
    async getEmployeesList() {
      const res = await getEmployeesListApi({
        page: this.currentPage,
        size: this.pageSize,
      });
      // console.log(res)
      // 保存数据
      this.tableData = res.data.rows;
      this.total = res.data.total;
    },
  },
  created() {
    // 得到所有的用户信息
    this.getEmployeesList();
    // 获取所有的角色列表
    this.getAllRole();
  },
  components: {
    mypanel: () => import("./components/mypanel.vue"),
  },
};
</script>

<style lang="scss">
.employees {
  margin: 20px;
  .box-card {
    margin-top: 20px;
  }
  .el-pagination {
    margin-top: 20px;
    text-align: right;
  }
  .myimg {
    width: 50px;
    height: 50px;
    border-radius: 50%;
    text-align: center;
  }
  //  td.el-table_1_column_3 {
  //     text-align: center;
  //     line-height: 50px;
  // }
  .cell {
    text-align: center;
  }
  .mydialog {
    text-align: center;
  }
}
</style>
